﻿using AutoMapper;
using BackData;
using Details.API.Application.Command.Goods;
using Domain;
using Infratructure.RepositoryWay;
using MediatR;

namespace Details.API.Application.Handler.Goods
{
    // 删除商品
    public class DelGoodsHandler : IRequestHandler<DelGoodsCommand, APIResult<int>>
    {
        private readonly IRepository<GoodsModel> goodway;
        private readonly ILogger<DelGoodsHandler> log;
        private readonly IMapper mapper;

        public DelGoodsHandler(IRepository<GoodsModel> goodway, ILogger<DelGoodsHandler> log, IMapper mapper)
        {
            this.goodway = goodway;
            this.log = log;
            this.mapper = mapper;
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="request">id</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        public async Task<APIResult<int>> Handle(DelGoodsCommand request, CancellationToken cancellationToken)
        {
            var res = new APIResult<int>();
            var value = await goodway.GetByIdAsync(request.Id);
            value.IsDel = true;
            var result = await goodway.UpdateAsync(value);
            if (result > 0)
            {
                res.Message = $"商品{value.Goodsname}删除成功";
                log.LogInformation($"商品{value.Goodsname}删除成功");
                res.Code = StateCode.OK;
                res.Data = result;
            }
            else
            {
                res.Message = $"商品{value.Goodsname}删除失败";
                log.LogError($"商品{value.Goodsname}删除失败");
                res.Code = StateCode.OK;
            }
            return res;
        }



    }

}
